﻿@using SQLiteCoreDB.Models;
@{
    ViewBag.Title = "文件列表";
    var list = ViewBag.listMy as List<FileInfoMy>;

}
<style>
    tr:hover {
        background: #cccccc;
    }
</style>
<div style="margin-bottom:100px;">

    @if (LoginMode.LV != "1" || LoginMode.admin)
    {

        <table class="layui-table" lay-skin="line">
            <thead>
                <tr>
                    <th>时间</th>
                    <th>大小</th>
                    <th>文件 </th>
                    @if (LoginMode.admin)
                    {
                        <th>操作</th>
                    }

                </tr>
            </thead>
            <tbody id="list_box">
                @Html.Partial("../Home/ModeList")
            </tbody>
        </table>

        <div style="display:none; " id="win_mkdir">
            <div style=" padding:30px;">
                <div style="font-size:12px;margin-bottom:30px;">文件夹名称，（对于英文符号）不允许包含\ / : * ? “ < > |</div>
                <div>
                    <input type="text" id="txtFolder" placeholder="文件夹名称" class="layui-input" oninput="checkVal(this);" />
                </div>
                <div style="margin-top:30px;text-align:center;">
                    <input type="button" value="确定" class="layui-btn layui-btn-lg layui-btn-normal" onclick="doMkdir(this)" />
                </div>
            </div>
        </div>




    }
    else
    {
        <h1 style="width: 320px; position: absolute; top: 28%; bottom: 0; left: 0; right: 0; margin: auto; ">
            无权查看！等待审核
        </h1>
    }
</div>


<script type="text/javascript">
  const delFile = async (file, dir) => {
        try {
            const { data: result } = await axios.get('/Home/DelFile', { params: { file } });
            if (result.Code === 200) {
                await GetFileList(dir);
                layui.layer.msg(result.Msg);
            } else {
                layui.layer.msg(result.Msg);
            }
        } catch (error) {
            layui.layer.msg('请求发生错误');
        }
    };

    async function GetFileList(dirNew) {
        let response = null;

        if (dirNew === null || dirNew === "") {
            response = await axios.post('/Home/GetFileList');
        } else {
            response = await axios.get('/Home/SeeDir', {
                params: {
                    dirNew: dirNew,
                    Dir: 1
                }
            })
        }

        if (response != null) {
            $("#list_box").html(response.data);
        }

    };
    layui.use(['layer', 'util'], async function () {
        const layer = layui.layer;
        const util = layui.util;

        util.on('lay-on', {
            page() {
                layer.open({
                    type: 1,
                    shadeClose: true,
                    area: ['480px', '280px'],
                    title: "创建文件夹",
                    content: $("#win_mkdir").html(),
                });
            },
            iframe() {
                layer.open({
                    type: 2,
                    title: '上传文件',
                    shadeClose: false,
                    shade: 0.8,
                    area: ['60%', '70%'],
                    content: `/file/Upload?dir=@Html.Raw(ViewBag.dirNew)`
                });
            },
        });
    });


    const checkVal = (s) => {
        const invalidChars = /[\\"/:*?<>|]/g;
        s.value = s.value.replace(invalidChars, '');
    };

    const doMkdir = async (button) => {
        const name = $(".layui-layer-content #txtFolder").val().trim();
        if (!name) {
            layui.layer.msg("文件夹名称不可为空");
            return;
        }
        $(button).prop("disabled", true);
        try {
            const { Code, Msg } = await $.ajax({
                url: `@Url.Action("MkdirFolder", "file")`,
                type: "post",
                data: { folder: name, dir: `@Html.Raw(ViewBag.dirNew)` }
            });

            if (Code === 500) {
                $(button).removeAttr("disabled");
                layui.layer.alert(`出错：${Msg}`);
            } else {
                layui.layer.msg(Msg);
                if (Code === 200) {
                    await GetFileList(`@Html.Raw(ViewBag.dirNew)`);
                }
            }
        } catch (error) {
            console.error('创建文件夹失败:', error);
        }
    };
</script>